<?php

namespace App\Console\Commands;

use App\Constant\QueueKey;
use App\Events\LoanRequestApplyEvent;
use App\Events\LoanRequestRetryEvent;
use App\Events\UserLoanApplyEvent;
use App\Exceptions\GeneralException;
use App\Facades\CommonPlatformRequestFacade;
use App\Facades\MoneyPlatformRequestFacade;
use App\Jobs\GetLoanResultJobQueue;
use App\Library\Guzzle\MoneyPlatformRequest;
use App\Models\BankCode;
use App\Models\LoanApply;
use App\Models\RepaySummary;
use App\Models\SettingRepayRate;
use App\Models\User;
use App\Models\UserBankCard;
use App\Models\UserChangeBankCardRecord;
use App\Models\UserGlobal;
use App\Models\UserLoanRequest;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class RetryLoanApply extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'retry-apply:cron {action : retry|test|makeNewTrans|makeTrans|loanRequest|retryLoan|loanApply}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '超额，凌晨重试放款';

    /**
     * The console command action. month|common|money|help
     *
     * @var string
     */
    protected $action;


    /**
     * Description: 重试放款先重试授信
     * Author: cg
     * Date: 2019-3-30 11:00
     */
    public function loanRequest()
    {
        $list = UserLoanRequest::where(['auto_loan_apply' => UserLoanRequest::AUTO_LOAN_APPLY_YES,'retry'=>1,'status'=>3])->get();
        if($list->count()>0){
            Log::debug("(重试放款先重试授信)查询到需要重试授信的数据{$list->count()}");
            foreach ($list as $index => $loan){
                Log::debug("(重试放款先重试授信)本次列表第{$index}个：{$loan->id}");
                Log::debug("(重试放款先重试授信)具体数据：".json_encode($loan,JSON_UNESCAPED_UNICODE));
                event(new LoanRequestApplyEvent($loan));
            }
        }
    }

    /**
     * Description: 超额，凌晨重试放款
     * Author: cg
     * Date: 2019-3-25 17:00
     * @throws \App\Exceptions\GeneralException
     */
    public function retry()
    {
        //获取前一天时间
        //开始：前一天开始时间
        $mtime = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
       //结束：前一天结束时间
        $btime = date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d')-1,date('Y')));

        /*$loanApplys = LoanApply::where('loan_status', LoanApply::LOAN_RETRY_APPLY)
            ->where('updated_at', '>', '2019-04-01 00:00:00')->get();*/
        $loanApplys = LoanApply::where(['loan_status'=>LoanApply::LOAN_RETRY_APPLY])->where([
            ['updated_at','>',$mtime],
            ['updated_at','<=',$btime]
        ])->get();
        //dd(count($loanApplys));


        foreach ($loanApplys as $apply) {
            $user = User::find($apply->user_id);
            // 取出用户当前已经重试授信成功的记录
            $loanRequest = UserLoanRequest::where('user_id', $user->id)
                ->where('status', UserLoanRequest::STATUS_SUCCESS)
                ->where('current', UserLoanRequest::CURRENT_NOW)
                ->where('auto_loan_apply', UserLoanRequest::AUTO_LOAN_APPLY_REQUEST)
                ->select('id', 'user_id', 'channel', 'actually_loan_money', 'actually_period', 'credit_id', 'pre_insurance_amt', 'cash_card_choice_id')
                ->orderByDesc('id')
                ->first();
            $cardInfo = UserBankCard::find($loanRequest->cash_card_choice_id);
            if (!$cardInfo) {
                Log::debug('凌晨重试未检测到您的收款银行卡，请您选择收款银行卡后重试');
                continue;
            }
            $cardId = $cardInfo->id;

            $params = [
                'pre_insurance_amt' => doubleval($loanRequest->pre_insurance_amt / 100),
                'credit_id' => $loanRequest->credit_id,
                'amount' => doubleval($loanRequest->actually_loan_money / 100),
                'period' => $loanRequest->actually_period,
            ];
            $res = MoneyPlatformRequestFacade::applyLoan($params);
            Log::debug('「 凌晨重试放款用户id为' . $user->id . '使用授信id为' . $loanRequest->credit_id . '银行卡id为' . $cardId . '进行放款申请时。资方放款返回值 」' . json_encode($res, JSON_UNESCAPED_UNICODE));
            if (empty($res) || empty($res['code'])) {
                Log::debug('凌晨重试请求失败');
                continue;
            }
            if ($res['code'] !== '01') {
                Log::debug('凌晨重试申请放款失败' . $res['message']);
                continue;
            }
            $apply->loan_request_id = $loanRequest->id; //授信id
            $apply->user_id = $user->id;
            $apply->pre_insurance_amt = doubleval($loanRequest->pre_insurance_amt / 100);  //授信额度的前置险
            $apply->trans_money_list = '';  //分账明细
            $apply->bank_card_id = $cardId;  //申请放款的银行卡
            $apply->out_trade_id = !empty($res['data']['outTradeId']) ? $res['data']['outTradeId'] : '';
            $apply->amount = doubleval($loanRequest->actually_loan_money / 100);  //授信表中 申请的借款金额（未扣除前置险）
            $apply->period = $loanRequest->actually_period; //授信表中 申请的分期期限
            $apply->loan_sign = !empty($res['sign']) ? $res['sign'] : '';
            $apply->message = !empty($res['message']) ? $res['message'] : ''; //返回的信息
            $status = !empty($res['data']['status']) ? $res['data']['status'] : $res['subCode'];
            if (!$status) {
                return failed('凌晨重试申请放款失败，数据异常');
            }
            $apply->sub_code = $status;
            $apply->loan_status = LoanApply::getLoanStatusByRes($res);
            //费率配置
            if (!empty($res['subCode']) && $res['subCode'] == "F") {
                $message = !empty($res['message']) ? $res['message'] : '';
                if ($message) {
                    if (mb_strstr($message, "金额") !== false || mb_strstr($message, "超限") !== false || mb_strstr($message, "限额") !== false) {
                        $apply->loan_status = LoanApply::LOAN_RETRY_APPLY;
                    }
                }
            }
            $getReplyRate = SettingRepayRate::getRateByChannl($loanRequest->channel, $loanRequest->actually_period);
            foreach ($getReplyRate as $key => $value) {
                if (in_array($key, LoanApply::RATE_KEY)) {
                    $apply->$key = $value;
                }
            }
            $data = !empty($res['data']) ? $res['data'] : [];
            //新流订单id,还款时使用
            if (!empty($data['loanId'])) {
                $apply->loan_id = $data['loanId'];
            }
            //资方返回的分期数
            if (!empty($data['period'])) {
                $apply->period = $data['period'];
            }
            //资方放款的申请金额
            if (!empty($data['amount'])) {
                $apply->amount = $data['amount'];
            }

            if (!empty($data['loanTime'])) {
                $apply->loan_time = $data['loanTime'];
            }
            //资方返回的明细
            if (!empty($data['transMoneyList'])) {
                $apply->trans_money_list = $data['transMoneyList'];
                //获取放款明细详情
                $detail = LoanApply::getDetailByTransMoneyList($data['transMoneyList']);
                if ($detail['arrival_account']) {
                    $apply->arrival_account = $detail['arrival_account'];
                }
                if ($detail['pre_insurance_amt']) {
                    $apply->pre_insurance_amt = $detail['pre_insurance_amt'];
                }
                if ($detail['performance_insurance']) {
                    $apply->performance_insurance = $detail['performance_insurance'];
                }
            }
            try {
                if($apply instanceof LoanApply){
                    $apply->save();
                }else{
                    Log::debug('「凌晨重试申请放款时」由于LoanApply错误导致放款结果异常');
                }
            } catch (\Exception $e) {
                Log::debug('「凌晨重试申请放款时」由于' . $e->getMessage() . '错误导致放款结果异常');
                continue;
            }
            event(new UserLoanApplyEvent($apply));
            if ($apply->loan_status == LoanApply::LOAN_STATUS_PROCESSING) {
                //任务三分钟后加入队列处理，获取放款处理结果
                GetLoanResultJobQueue::dispatch($apply)->onQueue(QueueKey::QUEUE_GET_LOAN_RESULT)->delay(Carbon::now()->addMinutes(3));
            }
        }

    }


    /**
     * 重试授信之前重新生成流水号
     */
    public function makeNewTrans()
    {
//        $loans = UserLoanRequest::whereIn('user_id', $uids)->where('current', 1)->where('status',2)->get();
//        $loanApply = new LoanApply();
//        $loanApply->setConnection('backend');
//        $applys1 = $loanApply->where('loan_status', 99)->where('created_at', '>=', "2019-03-29 00:00:00")
//            ->pluck('loan_request_id')->toArray();
//        $applys2 = $loanApply->where('message',"此笔贷款不在合同签订环节！")
//            ->pluck('loan_request_id')->toArray();
//        $applys3 = array_merge($applys1, $applys2);
//        $loans = UserLoanRequest::whereIn('id', $applys3)->where('current', 1)->get();
//        $loans = UserLoanRequest::where('id', 634158)->get();
//        foreach ($loans as $loan){
//            event(new LoanRequestRetryEvent($loan, true));
//        }
//        $this->info('makeNewTrans done.');
    }


    /**
     * ---------
     */
    public function makeTrans(){
        $nfsp_ids = [
            'cd47ef7feb564d3a8336e01e3569c039',
            '0d8d4fcaecc64fca9034d5fe8d191571',
            '0f848768314e48669652b307c9e6b5cc',
            '1018a940e6e34e7fb6c5da3d3222ca16',
            '10faa9c44c264de98142563d32597524',
            '127b6d2b713a4bd7b2c83644d70996e3',
            '16d6a481b5f64170a2ae371daab93138',
            '1c3c479dfeb649c1b8d2fd82fb7962d9',
            '1cda66fb1802428ebbf90b831dc5129f',
            '233b8c120cdb4f038d1b4acf4d9f227f',
            '23d922e2918648ed8e7ef88489015672',
            '2918a0dd247a474abc7977a0068bd2e9',
            '2c20afddfa584714b39f5f0e7cf6f94c',
            '2fc48a01392e4ea69dbbc2fcc9dd57f5',
            '324445c917994f32b4de4e10e586a115',
            '387f4008ca9f4ea589fe380e87af7d61',
            '5127b51b31da4f60805d98f70ee11b1e',
            '556a2f9ec73b429f97aca43276e88894',
            '708b3c87b9dd4e5cbb4b5aee2229f88c',
            '796fdf7487714f498b5917119b62006a',
            '7f4f331b04fa4dca91b24e524da02448',
            '85ce6f41c6fe44f29b942849b87b2376',
            '8799d27cc3634c0fbc299b06f1cd33b5',
            '8b2771c5f1994964a04005328a2b8df9',
            '94b4ed1aa71447fc819a6e4c0b338771',
            'a291b04ba25c43fc99d0fbca4452b78d',
            'ad25a205a1b34ab88e83c39bfa9ece8e',
            'c693fb0b1c18438fa198db58eefaecb8',
            'cabcf7b651784eb98162d3b46a585138',
            'cb5cef8889e045d5bbc2fb785c81be9b',
            'dd5c16d3c91d4da7b10f9f99131b8f56',
            'e03f50c9c9844f538e417890c7b4ed84',
            'f2d4b965d0a944e9b11979c8bc19ef66',
            'ce305ee2c3ee434ba42c3eeec076873d',
            '5269096b7ee24f929d81bb0f59641b5d',
            '16eb2141fa8f4499b4324388383488a6',
            'cde264fc5fd6464cac27a75549cf5d1d',
            'c3fbc2abc0ef4ef68faf06fde2e104d2',
            '420b76c8aef84091a0532c5904c4b689',
            '9786c679839e4e58b5ca7b3488be1b8f',
            'd80ee2092c4d4424b2f7d860ca00bb29',
            '177893f7899f4c22a22c9f9f746828ca',
            '3d9bd16b5f494d599a7783df2254338c',
            '9da23d254e3e415bb3718cda74cb7eae',
            '51535c8ddee24420bc8271fa03c97c41',
            '6a61820a61a5469e8e8bc1f10c844e96',
            '5cf85f4c4f744dfeab65b7f955d06563',
            '9a3c1f923a404771999372e47dde0f97',
            '90595700a0b44bfca81ee388d3137aeb',
            'fb58f84ddb38483488a886b4b839d4b6',
            '801b7188d7cd41709710522a604bf9a0',
            'f9196f74c9d145e2bd076f5ca3e773db',
            '471f88dd92834a5482e6af0430a354a9',
            '70f64c5aef934f9ab0ea7a3e123395c4',
            '6c8f0abfbbff4c8f9bebb89a53a03228',
            '43461fd06c7143f89f65cbe1946fe3ae',
            'bb72645b3b094b95b0b9f01b4a1dcb2e',
            'bfab50b9ff33452bb0030596da6ff385',
            '3546fc41b13943e289f7fc3044ff8fe5',
            'feef1afa9154411b8fb6a6820ebd0de0',
            '29f726bd60564b63880fb6df13068eb8',
            '22caa2e7ef604114a85e391f39f97325',
            'c164ec04876c444dba4f89b43903bf8a',
            'f70d0f41c2bd46b9bdaba21adfcd7800',
            'e558b3987a5e44748b981e6b33e37371',
            '5a8c71a3630e426293af9985b4332983',
            '0f5c4707b1c04683855dd7b842be1125',
            '908901b7b88144dbbc93d729e5b7397a',
            'b803f6f486784cf7a8bb7ae7b061f9c8',
            '7559f38f691e488894acb53224fd84df',
            'a724ba6e3708423b8ee498ad4d062ed9',
            '8ad41dd6643e4b779cbf9c59d50209cf',
            '9daffc6a1d464b30944168653157b4ce',
            '5a16ee343b414c218a4143619f41a8b9',
            '4dba8eb19fd1475086d70b77bb0613e3',
            'bf9317b3e39f4880970ed28e754920ee',
            'b18f70e5bccf40d99d023891708d804f',
            '73fc3dcd3ba2424eb0d78b58454acb8d',
            'fa17783a29f547c6be6a1612333c390f',
            'e46d815b136246d1a8575e4eb373e13b',
            '7b09841a99554dc2af204dac950c37f2',
            '9e81721bb9604d43831a72b23f2fa0b7',
            'b2e7b9d4e8cb411899fef27498b928c0',
            '746c1a9398574e249808445a8856acb8',
            'fd6a7d2bf7a543b0aa681b1ec0bc0b67',
            '72ed95a9a1744d29aac56ba007269948',
            'f58ef145f47245faa29663f00b4fde14',
            '9e583a20297b400ca1d351cb331c363f',
            '5a08691a9e664b8eaf4a7477bbf588cf',
            'b44e0ce3abcf4ba8abcd16c05f5ec6e3',
            'a324f9ed0cac4eee90ae933b6b41b4fb',
            '4aabdf97b7b24ba3910449351c5fa098',
            '8ca7d06ddff84a73b034e6ac254f2d3c',
            'c21dc475246943bd9c7a7e653d73188b',
            'c18bf08e21034a7ea9f91720fa7999a3',
            '9c0c86c8baca4b9fadd55615c512a13e',
            '35c09d6671144bfba761e77a902a4628',
            '9337a3d33f1c47859459237a14f6ac48',
            '34afe0d52a424a68a3d92e61bcc7d84a',
            'e40ea1c6a5b846b4a890deca14f36508',
            '7a1ebb61495945cfa30dbc1786dc76e9',
            '941deb0d6733418e879d53cba1a4634c',
            'f3469382ea414c029d861e770997d8ab',
            '091af70b8a5c4347bbdf2edf02671fa4',
            'c91452bb62b745159945f488d0437f21',
            '7c37c9efe49346f0bbe60725ecb451b1',
            'b6d7d20843d04651802c60dacbbcd38c',
            '6df348101d7c4e499987f7c731bbb2f6',
            'f8aac0259200439e818c47796308c06f',
            '0f086b5f24854f55b1be6719a5fac510',
            'c7975f55772547919312852ec4ca5675',
            '2ecdcb0f469a4ffcb8818f258810f9fc',
            '44ca1ea175a64ce7bec2504dee6d9a82',
            '3ee53197e8b7492d86ed90a7c1e10976',
            '7f13b23fc2314cc99da8ac8602a35c96',
            '16a9a6cdd0764de59b8d7dc186ad3178',
            '92029128d0454b45bf3d81b703c05545',
            '4183259e36e943e3bba0083ed151fc73',
            '87144ea32d184bd8b2cbbe5cec7b6195',
            '6330e9942eb74d96880d528cb4ea0ba9',
            '78b9b01427234e7495bda05305ad7085',
            'cf4e4c3462ff4de6a2582f4e5d237b78',
            'c3126359562349c68ae5b3f648dc24b3',
            '58d6f5801e6b47ceaaf7e55f132e65d3',
            '2fa683ab41fe484594cbeefcb4f6f32f',
            '4ad4f1f435a048248958ae10dfb83ec5',
            'e0f239dbf89a45b5b58aae66cd4a20a7',
            'a05ac2d4948d4d07b351d26d3f720973',
            '2d5e52ea11734721b804d52bea6cf9a3',
            '267f034dbf8340f38684ef7aad070f51',
            '8a582d72cfbf4dca922f8728a8d8c81b',
            'b1f3ded5557f4e44b83126ea4c0549d8',
            '96245fa18f04458a8959f6524681cdf8',
            '697e43fed95b4201b04df7ba51ae1da6',
            '9cda470a936243398c95f84950d0b959',
            '44b4d0c5a461459893a320d2bc02ff4a',
            '1806e180ea664822a38c4a01c3859316',
            '71f15b86049445739fe76a88320a16ca',
            '85bd9556c9d14b1a8c352b0cac142e8a',
            '184c515914f546da831ec90657a093bf',
            '46894740f8674ab6a9bf89a81b0414c2',
            '3eb9ecfa3576470f8460cc8447d3502c'
        ];
        $uids = User::whereIn('nfsp_user_id', $nfsp_ids)->pluck('id')->toArray();
        $loans = UserLoanRequest::whereIn('user_id', $uids)->where('current', 1)
            ->where('status',2)->get();
        foreach ($loans as $loan){
            event(new LoanRequestRetryEvent($loan, true));
        }
        $this->info('makeNewTrans done.');
    }

    public function retryLoan(){
        $nfsp_ids = [
            'cd47ef7feb564d3a8336e01e3569c039',
            '0d8d4fcaecc64fca9034d5fe8d191571',
            '0f848768314e48669652b307c9e6b5cc',
            '1018a940e6e34e7fb6c5da3d3222ca16',
            '10faa9c44c264de98142563d32597524',
            '127b6d2b713a4bd7b2c83644d70996e3',
            '16d6a481b5f64170a2ae371daab93138',
            '1c3c479dfeb649c1b8d2fd82fb7962d9',
            '1cda66fb1802428ebbf90b831dc5129f',
            '233b8c120cdb4f038d1b4acf4d9f227f',
            '23d922e2918648ed8e7ef88489015672',
            '2918a0dd247a474abc7977a0068bd2e9',
            '2c20afddfa584714b39f5f0e7cf6f94c',
            '2fc48a01392e4ea69dbbc2fcc9dd57f5',
            '324445c917994f32b4de4e10e586a115',
            '387f4008ca9f4ea589fe380e87af7d61',
            '5127b51b31da4f60805d98f70ee11b1e',
            '556a2f9ec73b429f97aca43276e88894',
            '708b3c87b9dd4e5cbb4b5aee2229f88c',
            '796fdf7487714f498b5917119b62006a',
            '7f4f331b04fa4dca91b24e524da02448',
            '85ce6f41c6fe44f29b942849b87b2376',
            '8799d27cc3634c0fbc299b06f1cd33b5',
            '8b2771c5f1994964a04005328a2b8df9',
            '94b4ed1aa71447fc819a6e4c0b338771',
            'a291b04ba25c43fc99d0fbca4452b78d',
            'ad25a205a1b34ab88e83c39bfa9ece8e',
            'c693fb0b1c18438fa198db58eefaecb8',
            'cabcf7b651784eb98162d3b46a585138',
            'cb5cef8889e045d5bbc2fb785c81be9b',
            'dd5c16d3c91d4da7b10f9f99131b8f56',
            'e03f50c9c9844f538e417890c7b4ed84',
            'f2d4b965d0a944e9b11979c8bc19ef66',
            'ce305ee2c3ee434ba42c3eeec076873d',
            '5269096b7ee24f929d81bb0f59641b5d',
            '16eb2141fa8f4499b4324388383488a6',
            'cde264fc5fd6464cac27a75549cf5d1d',
            'c3fbc2abc0ef4ef68faf06fde2e104d2',
            '420b76c8aef84091a0532c5904c4b689',
            '9786c679839e4e58b5ca7b3488be1b8f',
            'd80ee2092c4d4424b2f7d860ca00bb29',
            '177893f7899f4c22a22c9f9f746828ca',
            '3d9bd16b5f494d599a7783df2254338c',
            '9da23d254e3e415bb3718cda74cb7eae',
            '51535c8ddee24420bc8271fa03c97c41',
            '6a61820a61a5469e8e8bc1f10c844e96',
            '5cf85f4c4f744dfeab65b7f955d06563',
            '9a3c1f923a404771999372e47dde0f97',
            '90595700a0b44bfca81ee388d3137aeb',
            'fb58f84ddb38483488a886b4b839d4b6',
            '801b7188d7cd41709710522a604bf9a0',
            'f9196f74c9d145e2bd076f5ca3e773db',
            '471f88dd92834a5482e6af0430a354a9',
            '70f64c5aef934f9ab0ea7a3e123395c4',
            '6c8f0abfbbff4c8f9bebb89a53a03228',
            '43461fd06c7143f89f65cbe1946fe3ae',
            'bb72645b3b094b95b0b9f01b4a1dcb2e',
            'bfab50b9ff33452bb0030596da6ff385',
            '3546fc41b13943e289f7fc3044ff8fe5',
            'feef1afa9154411b8fb6a6820ebd0de0',
            '29f726bd60564b63880fb6df13068eb8',
            '22caa2e7ef604114a85e391f39f97325',
            'c164ec04876c444dba4f89b43903bf8a',
            'f70d0f41c2bd46b9bdaba21adfcd7800',
            'e558b3987a5e44748b981e6b33e37371',
            '5a8c71a3630e426293af9985b4332983',
            '0f5c4707b1c04683855dd7b842be1125',
            '908901b7b88144dbbc93d729e5b7397a',
            'b803f6f486784cf7a8bb7ae7b061f9c8',
            '7559f38f691e488894acb53224fd84df',
            'a724ba6e3708423b8ee498ad4d062ed9',
            '8ad41dd6643e4b779cbf9c59d50209cf',
            '9daffc6a1d464b30944168653157b4ce',
            '5a16ee343b414c218a4143619f41a8b9',
            '4dba8eb19fd1475086d70b77bb0613e3',
            'bf9317b3e39f4880970ed28e754920ee',
            'b18f70e5bccf40d99d023891708d804f',
            '73fc3dcd3ba2424eb0d78b58454acb8d',
            'fa17783a29f547c6be6a1612333c390f',
            'e46d815b136246d1a8575e4eb373e13b',
            '7b09841a99554dc2af204dac950c37f2',
            '9e81721bb9604d43831a72b23f2fa0b7',
            'b2e7b9d4e8cb411899fef27498b928c0',
            '746c1a9398574e249808445a8856acb8',
            'fd6a7d2bf7a543b0aa681b1ec0bc0b67',
            '72ed95a9a1744d29aac56ba007269948',
            'f58ef145f47245faa29663f00b4fde14',
            '9e583a20297b400ca1d351cb331c363f',
            '5a08691a9e664b8eaf4a7477bbf588cf',
            'b44e0ce3abcf4ba8abcd16c05f5ec6e3',
            'a324f9ed0cac4eee90ae933b6b41b4fb',
            '4aabdf97b7b24ba3910449351c5fa098',
            '8ca7d06ddff84a73b034e6ac254f2d3c',
            'c21dc475246943bd9c7a7e653d73188b',
            'c18bf08e21034a7ea9f91720fa7999a3',
            '9c0c86c8baca4b9fadd55615c512a13e',
            '35c09d6671144bfba761e77a902a4628',
            '9337a3d33f1c47859459237a14f6ac48',
            '34afe0d52a424a68a3d92e61bcc7d84a',
            'e40ea1c6a5b846b4a890deca14f36508',
            '7a1ebb61495945cfa30dbc1786dc76e9',
            '941deb0d6733418e879d53cba1a4634c',
            'f3469382ea414c029d861e770997d8ab',
            '091af70b8a5c4347bbdf2edf02671fa4',
            'c91452bb62b745159945f488d0437f21',
            '7c37c9efe49346f0bbe60725ecb451b1',
            'b6d7d20843d04651802c60dacbbcd38c',
            '6df348101d7c4e499987f7c731bbb2f6',
            'f8aac0259200439e818c47796308c06f',
            '0f086b5f24854f55b1be6719a5fac510',
            'c7975f55772547919312852ec4ca5675',
            '2ecdcb0f469a4ffcb8818f258810f9fc',
            '44ca1ea175a64ce7bec2504dee6d9a82',
            '3ee53197e8b7492d86ed90a7c1e10976',
            '7f13b23fc2314cc99da8ac8602a35c96',
            '16a9a6cdd0764de59b8d7dc186ad3178',
            '92029128d0454b45bf3d81b703c05545',
            '4183259e36e943e3bba0083ed151fc73',
            '87144ea32d184bd8b2cbbe5cec7b6195',
            '6330e9942eb74d96880d528cb4ea0ba9',
            '78b9b01427234e7495bda05305ad7085',
            'cf4e4c3462ff4de6a2582f4e5d237b78',
            'c3126359562349c68ae5b3f648dc24b3',
            '58d6f5801e6b47ceaaf7e55f132e65d3',
            '2fa683ab41fe484594cbeefcb4f6f32f',
            '4ad4f1f435a048248958ae10dfb83ec5',
            'e0f239dbf89a45b5b58aae66cd4a20a7',
            'a05ac2d4948d4d07b351d26d3f720973',
            '2d5e52ea11734721b804d52bea6cf9a3',
            '267f034dbf8340f38684ef7aad070f51',
            '8a582d72cfbf4dca922f8728a8d8c81b',
            'b1f3ded5557f4e44b83126ea4c0549d8',
            '96245fa18f04458a8959f6524681cdf8',
            '697e43fed95b4201b04df7ba51ae1da6',
            '9cda470a936243398c95f84950d0b959',
            '44b4d0c5a461459893a320d2bc02ff4a',
            '1806e180ea664822a38c4a01c3859316',
            '71f15b86049445739fe76a88320a16ca',
            '85bd9556c9d14b1a8c352b0cac142e8a',
            '184c515914f546da831ec90657a093bf',
            '46894740f8674ab6a9bf89a81b0414c2',
            '3eb9ecfa3576470f8460cc8447d3502c'
        ];
        $uids = User::whereIn('nfsp_user_id', $nfsp_ids)->pluck('id')->toArray();
        $list = UserLoanRequest::whereIn('user_id', $uids)->where('current', 1)
            ->where('status',2)->where('retry',1)->where('auto_loan_apply', 1)->get();
        if($list->count()>0){
            Log::debug("(重试放款先重试授信)查询到需要重试授信的数据{$list->count()}");
            foreach ($list as $index => $loan){
                Log::debug("(重试放款先重试授信)本次列表第{$index}个：{$loan->id}");
                Log::debug("(重试放款先重试授信)具体数据：".json_encode($loan,JSON_UNESCAPED_UNICODE));
                event(new LoanRequestApplyEvent($loan));
            }
        }
    }

    public function loanApply(){
        $nfsp_ids = [
            'cd47ef7feb564d3a8336e01e3569c039',
            '0d8d4fcaecc64fca9034d5fe8d191571',
            '0f848768314e48669652b307c9e6b5cc',
            '1018a940e6e34e7fb6c5da3d3222ca16',
            '10faa9c44c264de98142563d32597524',
            '127b6d2b713a4bd7b2c83644d70996e3',
            '16d6a481b5f64170a2ae371daab93138',
            '1c3c479dfeb649c1b8d2fd82fb7962d9',
            '1cda66fb1802428ebbf90b831dc5129f',
            '233b8c120cdb4f038d1b4acf4d9f227f',
            '23d922e2918648ed8e7ef88489015672',
            '2918a0dd247a474abc7977a0068bd2e9',
            '2c20afddfa584714b39f5f0e7cf6f94c',
            '2fc48a01392e4ea69dbbc2fcc9dd57f5',
            '324445c917994f32b4de4e10e586a115',
            '387f4008ca9f4ea589fe380e87af7d61',
            '5127b51b31da4f60805d98f70ee11b1e',
            '556a2f9ec73b429f97aca43276e88894',
            '708b3c87b9dd4e5cbb4b5aee2229f88c',
            '796fdf7487714f498b5917119b62006a',
            '7f4f331b04fa4dca91b24e524da02448',
            '85ce6f41c6fe44f29b942849b87b2376',
            '8799d27cc3634c0fbc299b06f1cd33b5',
            '8b2771c5f1994964a04005328a2b8df9',
            '94b4ed1aa71447fc819a6e4c0b338771',
            'a291b04ba25c43fc99d0fbca4452b78d',
            'ad25a205a1b34ab88e83c39bfa9ece8e',
            'c693fb0b1c18438fa198db58eefaecb8',
            'cabcf7b651784eb98162d3b46a585138',
            'cb5cef8889e045d5bbc2fb785c81be9b',
            'dd5c16d3c91d4da7b10f9f99131b8f56',
            'e03f50c9c9844f538e417890c7b4ed84',
            'f2d4b965d0a944e9b11979c8bc19ef66',
            'ce305ee2c3ee434ba42c3eeec076873d',
            '5269096b7ee24f929d81bb0f59641b5d',
            '16eb2141fa8f4499b4324388383488a6',
            'cde264fc5fd6464cac27a75549cf5d1d',
            'c3fbc2abc0ef4ef68faf06fde2e104d2',
            '420b76c8aef84091a0532c5904c4b689',
            '9786c679839e4e58b5ca7b3488be1b8f',
            'd80ee2092c4d4424b2f7d860ca00bb29',
            '177893f7899f4c22a22c9f9f746828ca',
            '3d9bd16b5f494d599a7783df2254338c',
            '9da23d254e3e415bb3718cda74cb7eae',
            '51535c8ddee24420bc8271fa03c97c41',
            '6a61820a61a5469e8e8bc1f10c844e96',
            '5cf85f4c4f744dfeab65b7f955d06563',
            '9a3c1f923a404771999372e47dde0f97',
            '90595700a0b44bfca81ee388d3137aeb',
            'fb58f84ddb38483488a886b4b839d4b6',
            '801b7188d7cd41709710522a604bf9a0',
            'f9196f74c9d145e2bd076f5ca3e773db',
            '471f88dd92834a5482e6af0430a354a9',
            '70f64c5aef934f9ab0ea7a3e123395c4',
            '6c8f0abfbbff4c8f9bebb89a53a03228',
            '43461fd06c7143f89f65cbe1946fe3ae',
            'bb72645b3b094b95b0b9f01b4a1dcb2e',
            'bfab50b9ff33452bb0030596da6ff385',
            '3546fc41b13943e289f7fc3044ff8fe5',
            'feef1afa9154411b8fb6a6820ebd0de0',
            '29f726bd60564b63880fb6df13068eb8',
            '22caa2e7ef604114a85e391f39f97325',
            'c164ec04876c444dba4f89b43903bf8a',
            'f70d0f41c2bd46b9bdaba21adfcd7800',
            'e558b3987a5e44748b981e6b33e37371',
            '5a8c71a3630e426293af9985b4332983',
            '0f5c4707b1c04683855dd7b842be1125',
            '908901b7b88144dbbc93d729e5b7397a',
            'b803f6f486784cf7a8bb7ae7b061f9c8',
            '7559f38f691e488894acb53224fd84df',
            'a724ba6e3708423b8ee498ad4d062ed9',
            '8ad41dd6643e4b779cbf9c59d50209cf',
            '9daffc6a1d464b30944168653157b4ce',
            '5a16ee343b414c218a4143619f41a8b9',
            '4dba8eb19fd1475086d70b77bb0613e3',
            'bf9317b3e39f4880970ed28e754920ee',
            'b18f70e5bccf40d99d023891708d804f',
            '73fc3dcd3ba2424eb0d78b58454acb8d',
            'fa17783a29f547c6be6a1612333c390f',
            'e46d815b136246d1a8575e4eb373e13b',
            '7b09841a99554dc2af204dac950c37f2',
            '9e81721bb9604d43831a72b23f2fa0b7',
            'b2e7b9d4e8cb411899fef27498b928c0',
            '746c1a9398574e249808445a8856acb8',
            'fd6a7d2bf7a543b0aa681b1ec0bc0b67',
            '72ed95a9a1744d29aac56ba007269948',
            'f58ef145f47245faa29663f00b4fde14',
            '9e583a20297b400ca1d351cb331c363f',
            '5a08691a9e664b8eaf4a7477bbf588cf',
            'b44e0ce3abcf4ba8abcd16c05f5ec6e3',
            'a324f9ed0cac4eee90ae933b6b41b4fb',
            '4aabdf97b7b24ba3910449351c5fa098',
            '8ca7d06ddff84a73b034e6ac254f2d3c',
            'c21dc475246943bd9c7a7e653d73188b',
            'c18bf08e21034a7ea9f91720fa7999a3',
            '9c0c86c8baca4b9fadd55615c512a13e',
            '35c09d6671144bfba761e77a902a4628',
            '9337a3d33f1c47859459237a14f6ac48',
            '34afe0d52a424a68a3d92e61bcc7d84a',
            'e40ea1c6a5b846b4a890deca14f36508',
            '7a1ebb61495945cfa30dbc1786dc76e9',
            '941deb0d6733418e879d53cba1a4634c',
            'f3469382ea414c029d861e770997d8ab',
            '091af70b8a5c4347bbdf2edf02671fa4',
            'c91452bb62b745159945f488d0437f21',
            '7c37c9efe49346f0bbe60725ecb451b1',
            'b6d7d20843d04651802c60dacbbcd38c',
            '6df348101d7c4e499987f7c731bbb2f6',
            'f8aac0259200439e818c47796308c06f',
            '0f086b5f24854f55b1be6719a5fac510',
            'c7975f55772547919312852ec4ca5675',
            '2ecdcb0f469a4ffcb8818f258810f9fc',
            '44ca1ea175a64ce7bec2504dee6d9a82',
            '3ee53197e8b7492d86ed90a7c1e10976',
            '7f13b23fc2314cc99da8ac8602a35c96',
            '16a9a6cdd0764de59b8d7dc186ad3178',
            '92029128d0454b45bf3d81b703c05545',
            '4183259e36e943e3bba0083ed151fc73',
            '87144ea32d184bd8b2cbbe5cec7b6195',
            '6330e9942eb74d96880d528cb4ea0ba9',
            '78b9b01427234e7495bda05305ad7085',
            'cf4e4c3462ff4de6a2582f4e5d237b78',
            'c3126359562349c68ae5b3f648dc24b3',
            '58d6f5801e6b47ceaaf7e55f132e65d3',
            '2fa683ab41fe484594cbeefcb4f6f32f',
            '4ad4f1f435a048248958ae10dfb83ec5',
            'e0f239dbf89a45b5b58aae66cd4a20a7',
            'a05ac2d4948d4d07b351d26d3f720973',
            '2d5e52ea11734721b804d52bea6cf9a3',
            '267f034dbf8340f38684ef7aad070f51',
            '8a582d72cfbf4dca922f8728a8d8c81b',
            'b1f3ded5557f4e44b83126ea4c0549d8',
            '96245fa18f04458a8959f6524681cdf8',
            '697e43fed95b4201b04df7ba51ae1da6',
            '9cda470a936243398c95f84950d0b959',
            '44b4d0c5a461459893a320d2bc02ff4a',
            '1806e180ea664822a38c4a01c3859316',
            '71f15b86049445739fe76a88320a16ca',
            '85bd9556c9d14b1a8c352b0cac142e8a',
            '184c515914f546da831ec90657a093bf',
            '46894740f8674ab6a9bf89a81b0414c2',
            '3eb9ecfa3576470f8460cc8447d3502c'
        ];
        $uids = User::whereIn('nfsp_user_id', $nfsp_ids)->pluck('id')->toArray();
        $loans  = UserLoanRequest::whereIn('user_id', $uids)->where('current', 1)->where('status',2)->get();
        $loanApplys = LoanApply::whereIn('loan_request_id', $loans)->get();
        foreach ($loanApplys as $apply) {
            $user = User::find($apply->user_id);
            // 取出用户当前已经重试授信成功的记录
            $loanRequest = UserLoanRequest::where('user_id', $user->id)
                ->where('status', UserLoanRequest::STATUS_SUCCESS)
                ->where('current', UserLoanRequest::CURRENT_NOW)
                ->where('auto_loan_apply', UserLoanRequest::AUTO_LOAN_APPLY_REQUEST)
                ->select('id', 'user_id', 'channel', 'actually_loan_money', 'actually_period', 'credit_id', 'pre_insurance_amt', 'cash_card_choice_id')
                ->orderByDesc('id')
                ->first();
            $cardInfo = UserBankCard::find($loanRequest->cash_card_choice_id);
            if (!$cardInfo) {
                Log::debug('凌晨重试未检测到您的收款银行卡，请您选择收款银行卡后重试');
                continue;
            }
            $cardId = $cardInfo->id;

            $params = [
                'pre_insurance_amt' => doubleval($loanRequest->pre_insurance_amt / 100),
                'credit_id' => $loanRequest->credit_id,
                'amount' => doubleval($loanRequest->actually_loan_money / 100),
                'period' => $loanRequest->actually_period,
            ];
            $res = MoneyPlatformRequestFacade::applyLoan($params);
            Log::debug('「 凌晨重试放款用户id为' . $user->id . '使用授信id为' . $loanRequest->credit_id . '银行卡id为' . $cardId . '进行放款申请时。资方放款返回值 」' . json_encode($res, JSON_UNESCAPED_UNICODE));
            if (empty($res) || empty($res['code'])) {
                Log::debug('凌晨重试请求失败');
                continue;
            }
            if ($res['code'] !== '01') {
                Log::debug('凌晨重试申请放款失败' . $res['message']);
                continue;
            }
            $apply->loan_request_id = $loanRequest->id; //授信id
            $apply->user_id = $user->id;
            $apply->pre_insurance_amt = doubleval($loanRequest->pre_insurance_amt / 100);  //授信额度的前置险
            $apply->trans_money_list = '';  //分账明细
            $apply->bank_card_id = $cardId;  //申请放款的银行卡
            $apply->out_trade_id = !empty($res['data']['outTradeId']) ? $res['data']['outTradeId'] : '';
            $apply->amount = doubleval($loanRequest->actually_loan_money / 100);  //授信表中 申请的借款金额（未扣除前置险）
            $apply->period = $loanRequest->actually_period; //授信表中 申请的分期期限
            $apply->loan_sign = !empty($res['sign']) ? $res['sign'] : '';
            $apply->message = !empty($res['message']) ? $res['message'] : ''; //返回的信息
            $status = !empty($res['data']['status']) ? $res['data']['status'] : $res['subCode'];
            if (!$status) {
                return failed('凌晨重试申请放款失败，数据异常');
            }
            $apply->sub_code = $status;
            $apply->loan_status = LoanApply::getLoanStatusByRes($res);
            //费率配置
            if (!empty($res['subCode']) && $res['subCode'] == "F") {
                $message = !empty($res['message']) ? $res['message'] : '';
                if ($message) {
                    if (mb_strstr($message, "金额") !== false || mb_strstr($message, "超限") !== false || mb_strstr($message, "限额") !== false) {
                        $apply->loan_status = LoanApply::LOAN_RETRY_APPLY;
                    }
                }
            }
            $getReplyRate = SettingRepayRate::getRateByChannl($loanRequest->channel, $loanRequest->actually_period);
            foreach ($getReplyRate as $key => $value) {
                if (in_array($key, LoanApply::RATE_KEY)) {
                    $apply->$key = $value;
                }
            }
            $data = !empty($res['data']) ? $res['data'] : [];
            //新流订单id,还款时使用
            if (!empty($data['loanId'])) {
                $apply->loan_id = $data['loanId'];
            }
            //资方返回的分期数
            if (!empty($data['period'])) {
                $apply->period = $data['period'];
            }
            //资方放款的申请金额
            if (!empty($data['amount'])) {
                $apply->amount = $data['amount'];
            }

            if (!empty($data['loanTime'])) {
                $apply->loan_time = $data['loanTime'];
            }
            //资方返回的明细
            if (!empty($data['transMoneyList'])) {
                $apply->trans_money_list = $data['transMoneyList'];
                //获取放款明细详情
                $detail = LoanApply::getDetailByTransMoneyList($data['transMoneyList']);
                if ($detail['arrival_account']) {
                    $apply->arrival_account = $detail['arrival_account'];
                }
                if ($detail['pre_insurance_amt']) {
                    $apply->pre_insurance_amt = $detail['pre_insurance_amt'];
                }
                if ($detail['performance_insurance']) {
                    $apply->performance_insurance = $detail['performance_insurance'];
                }
            }
            try {
                if($apply instanceof LoanApply){
                    $apply->save();
                }else{
                    Log::debug('「凌晨重试申请放款时」由于LoanApply错误导致放款结果异常');
                }
            } catch (\Exception $e) {
                Log::debug('「凌晨重试申请放款时」由于' . $e->getMessage() . '错误导致放款结果异常');
                continue;
            }
            event(new UserLoanApplyEvent($apply));
            if ($apply->loan_status == LoanApply::LOAN_STATUS_PROCESSING) {
                //任务三分钟后加入队列处理，获取放款处理结果
                GetLoanResultJobQueue::dispatch($apply)->onQueue(QueueKey::QUEUE_GET_LOAN_RESULT)->delay(Carbon::now()->addMinutes(3));
            }
        }
    }

    public function test(){
        $ret = DB::connection('backend')->select('select bank_code, bank_name, bank_name_show from user_bank_cards GROUP BY bank_code');
        foreach ($ret as $value){
            $bank_code = $value->bank_code;
            $bank_name = $value->bank_name;
            $bank_name_show = $value->bank_name_show;
            $arr = [
                'bank_code' => $bank_code,
                'bank_name' => $bank_name,
                'bank_name_show' => $bank_name_show
            ];
            DB::table('bank_codes')->insert($arr);
        }

    }



    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->initAction();
        $this->runAction();
    }


    /**
     * Description: Initialize command action.
     * Author: Gxs
     */
    protected function initAction()
    {
        $this->action = $this->argument('action');

        if (!in_array($this->action, ['retry', 'test','makeNewTrans','loanRequest','makeTrans', 'retryLoan','loanApply'])) {
            $this->error("Invalid argument '{$this->action}'. Expected .");
            exit(1);
        }
    }

    /**
     * Description: Run action.
     * Author: Gxs
     */
    protected function runAction()
    {
        $this->{$this->action}();
    }


}
